package com.zjf.addresschk.service;

import com.zjf.addresschk.entity.AddressEntity;
import com.zjf.addresschk.entity.InfAbstract;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.entity.enmus.ExcelStyleType;
import org.jeecgframework.poi.excel.entity.enmus.ExcelType;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:通用实现
 * @User: zhaojunfu
 * @Date: 2015/5/23
 * @Time: 17:06
 */
public abstract class AddressChkServiceBase implements  AddressChkService{
    @Override
    public List<AddressEntity> getAddressStr(String filepath) {
        ImportParams params = new ImportParams();
        params.setTitleRows(0);
        params.setHeadRows(1);
        List<AddressEntity> list = ExcelImportUtil.importExcel(new File(filepath), AddressEntity.class,
                params);
        return list;
    }

    @Override
    public abstract void checkAddress(List<AddressEntity> addressList) ;

    @Override
    public abstract void catchPostcode(List<AddressEntity> addressList);

    @Override
    public void outputResult(List<AddressEntity> addressList, String filepath) {
        ExportParams params = new ExportParams("检测结果", "测试", ExcelType.XSSF);
        params.setStyle(ExcelStyleType.COLOR.getClazz());
        Workbook workbook = ExcelExportUtil.exportExcel(params, AddressEntity.class, addressList);
        File savefile = new File(filepath);
        if(!savefile.exists()) {
            try {
                savefile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(savefile);
            workbook.write(fos);
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    protected String sendToHttp(String url){
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            // 创建httpget.
            HttpGet httpget = new HttpGet(url);
            // System.out.println("executing request " + httpget.getURI());
            // 执行get请求.
            CloseableHttpResponse response = httpclient.execute(httpget);
            try {
                // 获取响应实体
                HttpEntity entity = response.getEntity();
//                System.out.println("--------------------------------------");
                // 打印响应状态
//                System.out.println(response.getStatusLine());
                if (entity != null) {
                    // 打印响应内容长度
//                    System.out.println("Response content length: " + entity.getContentLength());
                    // 打印响应内容
                    String r = EntityUtils.toString(entity);
//                    System.out.println("Response content: " +r);
                    return r;
                }
//                System.out.println("------------------------------------");
            } finally {
                response.close();
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭连接,释放资源
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
